// 菜单组件
<template>
  <el-container>
    <el-header style="height: 50px">
      <div class="home_logo">
        <img src="../assets/image/home-logo.jpg" alt="" />
        <span>管理系统</span>
      </div>
      <el-button size="mini" type="primary" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isAsideFlex ? '64px' : '170px'">
        <div class="btn_flex" @click="asideFlex">| | |</div>
        <el-menu
          :collapse="isAsideFlex"
          active-text-color="#409EFF"
          unique-opened
          :collapse-transition="false"
          router
          :default-active="$route.path"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      isAsideFlex: false,
      menuList: [],
      iconList: [
        "el-icon-service",
        "el-icon-key",
        "el-icon-shopping-bag-2",
        "el-icon-shopping-cart-2",
        "el-icon-pie-chart",
      ],
    };
  },
  methods: {
    // 退出按钮
    exit() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 控制侧边栏的伸缩
    asideFlex() {
      this.isAsideFlex = !this.isAsideFlex;
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 15px 0 5px;
  background-color: rgb(250, 237, 237);
  .home_logo {
    display: flex;
    align-items: center;
    img {
      width: 40px;
      height: 40px;
      margin-right: 10px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
}
.el-aside {
  background-color: rgb(240, 240, 240);
  .btn_flex {
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: rgb(235, 255, 202);
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
    background-color: rgb(240, 240, 240);
  }
}
</style>